import * as packetListService from '../services/packetList';
import { message } from 'antd';

export default {
  namespace: 'packetList',
  state: {
    totalCount: 0,
  	list: [],
    materialList: []
  },
  reducers: {
    save(state, { payload: { list, totalCount} }) {
    	return { ...state, list, totalCount };
    },
    saveMaterial(state, { payload: { materialList} }) {
      return { ...state, materialList };
    }
  },
  effects: {
    *fetch({ payload }, { call, put }) {
    	const { data } = yield call(packetListService.fetch, { payload });
    	if (data.success && data.data) {
        const result = data.data;
    		yield put({
	    		type: 'save',
	    		payload: {
	    			list: result.list,
            totalCount: result.totalCount
	    		}
	    	});
        return;
    	}
      message.error(data.msg || '系统错误，请重试', 3);
    },
    *fetchMaterial({ payload }, { call, put }) {
    	const { data } = yield call(packetListService.fetchMaterial, { payload });
    	if (data) {
        const result = data.data;
    		yield put({
	    		type: 'saveMaterial',
	    		payload: {
	    			materialList: result.list
	    		}
	    	});
    	}
    }
  },
  subscriptions: {
  }
};
